package com.lam.web;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.lam.fundamental.R;

public class HttpClientActivity extends Activity{
	private Button requestButton;
	private ImageView imageView;
	private EditText editText;
	private URLConnection urlConnection;
	Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			imageView.setImageBitmap((Bitmap)msg.obj);
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web_httpclient);
		requestButton=(Button)findViewById(R.id.bt_httpclient_request);
		imageView=(ImageView)findViewById(R.id.iv_httpclient);
		editText=(EditText)findViewById(R.id.et_httpclient_url);
		
		requestButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				String url=editText.getText().toString();
//				if(url.equals("")){
//					Toast.makeText(HttpClientActivity.this, "Url required!", Toast.LENGTH_SHORT).show();
//					return;
//				}
				
				
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						
						try {
							URL url=new URL("http://img0.bdstatic.com/img/image/shouye/mingxing0720.jpg");
							urlConnection=url.openConnection();
							InputStream in = new BufferedInputStream(urlConnection.getInputStream());
							 Bitmap  bitmap = BitmapFactory.decodeStream(in);  
					          in.close();  
							Message msg=new Message();
							msg.what=0;
							msg.obj=bitmap;
							handler.sendMessage(msg);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						
					}
				}).start();
				
			
			
				
				
			}
		});
		
	}

}
